<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	<xsl:template match="/">
	<xsl:variable name="uppercase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
	<xsl:variable name="lowercase">abcdefghijklmnopqrstuvwxyz</xsl:variable>
		<html>
			<head>
				<title>Statistic Information</title>
				<link type="text/css" rel="stylesheet" href="../styles/style_xslt.css"/>
				<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.17.custom.css" rel="stylesheet" />	
				<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
				<script type="text/javascript" src="../js/jquery-ui-1.8.17.custom.min.js"></script>
				<script type="text/javascript">
						<![CDATA[
						function init(){
							var iframe=parent.window.document.getElementById('statisticFrame');
							if (iframe!=null){
								iframe.setAttribute('height',parseInt(getComputedStyle(document.body.parentNode).height)+50);
							}
						}
						            //Converter - convert data of cols into sortable type：String,int,float
			            function convert(sValue, sDataType) {
			                switch(sDataType) {
			                    case "int":
			                        return parseInt(sValue);
			                    case "float":
			                        return parseFloat(sValue);
			                    case "date":
			                        return new Date(Date.parse(sValue));
			                    default:
			                        return sValue.toString();
			                
			                }
			            }
			            
			            //Generator of Sort，iCol - col index，sDataType - datatype of the cols
			            function generateCompareTRs(iCol, sDataType) {
			        
			                return  function compareTRs(oTR1, oTR2) {
			                            var vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue, sDataType);
			                            var vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue, sDataType);
			        
			                            if (vValue1 < vValue2) {
			                                return -1;
			                            } else if (vValue1 > vValue2) {
			                                return 1;
			                            } else {
			                                return 0;
			                            }
			                        };
			            }
			            
			            //Sort function
			            function sortTable(sTableID, iCol, sDataType) {
			                var oTable = document.getElementById(sTableID);
			                var oTBody = oTable.tBodies[0];
			                var colDataRows = oTBody.rows;
			                var aTRs = new Array;
			                
			                //Put all Rows into array.
			                for (var i=0; i < colDataRows.length; i++) {
			                    aTRs[i] = colDataRows[i];
			                }
			                 
			                //judge if the last sort is the same with current.if yes,do reverse()
			                if (oTable.sortCol == iCol) {
			                    aTRs.reverse();
			                } else {
			                    //use the sort of array
			                    aTRs.sort(generateCompareTRs(iCol, sDataType));
			                }
			        
			                var oFragment = document.createDocumentFragment();
			                for (var i=0; i < aTRs.length; i++) {
			                    oFragment.appendChild(aTRs[i]);
			                }
			       
			                oTBody.appendChild(oFragment);
			                //record the sort result of the last time.
			                oTable.sortCol = iCol;
			            }
						]]>
				</script>
			</head>
			<body style="text-align:justify" onload="init();">
				<div style="clear:both;float:none;text-align:left">
					<form action="../StatisticApp?action=renew" method="post">
						<button onclick="this.parentNode.submit()">Renew!</button>
					</form>
				</div>
				<div id="thumb" style="vertical-align:middle;float:left;width:512px">
					<xsl:variable name="sqlSize"
						select="sum(/statistic/thumb[@mode='sql']/@size)" />
					<xsl:variable name="db4oSize"
						select="sum(/statistic/thumb[@mode='db4o']/@size)" />
					<xsl:variable name="sqlCount"
						select="sum(/statistic/thumb[@mode='sql']/@count)" />
					<xsl:variable name="db4oCount"
						select="sum(/statistic/thumb[@mode='db4o']/@count)" />
					<xsl:variable name="sqlTime"
						select="sum(/statistic/thumb[@mode='sql']/@time)" />
					<xsl:variable name="db4oTime"
						select="sum(/statistic/thumb[@mode='db4o']/@time)" />
					<xsl:variable name="sqlSpeed"
						select="format-number(($sqlSize div $sqlTime),'0.00')" />
					<xsl:variable name="db4oSpeed"
						select="format-number(($db4oSize div $db4oTime),'0.00')" />
					<xsl:variable name="maxSpeed">
						<xsl:choose>
							<xsl:when test="$sqlSpeed &gt; $db4oSpeed">
								<xsl:value-of select="$sqlSpeed" />
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$db4oSpeed" />
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<xsl:variable name="sqlHeight" select="$sqlSpeed div $maxSpeed * 200" />
					<xsl:variable name="db4oHeight" select="$db4oSpeed div $maxSpeed * 200" />
					<div>1.Loading thumbs</div>

					<div>
						<table border="1">
							<th></th>
							<th>SQL(Oracle)</th>
							<th>Object(DB4O)</th>
							<tr>
								<td>Load Size(Byte)</td>
								<td>
									<xsl:value-of select="$sqlSize" />
								</td>
								<td>
									<xsl:value-of select="$db4oSize" />
								</td>
							</tr>
							<tr>
								<td>Count(thumb)</td>
								<td>
									<xsl:value-of select="$sqlCount" />
								</td>
								<td>
									<xsl:value-of select="$db4oCount" />
								</td>
							</tr>
							<tr>
								<td>Load Time(ms)</td>
								<td>
									<xsl:value-of select="$sqlTime" />
								</td>
								<td>
									<xsl:value-of select="$db4oTime" />
								</td>
							</tr>
							<tr>
								<td>Speed(KB/s)</td>
								<td>
									<xsl:value-of select="$sqlSpeed" />
								</td>
								<td>
									<xsl:value-of select="$db4oSpeed" />
								</td>
							</tr>
						</table>
					</div>
					<div class="svg" style="width:450px;height:300px;">
						<svg width="100%" height="100%" version="1.1"
							xmlns="http://www.w3.org/2000/svg">
							<line x1="1" y1="30" x2="1" y2="250"
								style="stroke:rgb(5,15,192);stroke-width:1" />
							<line x1="1" y1="250" x2="350" y2="250"
								style="stroke:rgb(5,15,192);stroke-width:1" />
							<text x="0" y="20">
								Speed(KB/s)
							</text>
							<rect x="90" y="{50+200-$sqlHeight}" width="50" height="{$sqlHeight}"
								style="fill:blue;stroke:black;stroke-width:0;fill-opacity:1;stroke-opacity:0.9" />
							<text x="100" y="270">
								SQL
							</text>
							<text x="95" y="{45+200-$sqlHeight}">
								<xsl:value-of select="$sqlSpeed" />
							</text>
							<rect x="230" y="{50+200-$db4oHeight}" width="50" height="{$db4oHeight}"
								style="fill:orange;stroke:black;stroke-width:0;fill-opacity:1;stroke-opacity:0.9" />
							<text x="240" y="270">
								DB4O
							</text>
							<text x="235" y="{45+200-$db4oHeight}">
								<xsl:value-of select="$db4oSpeed" />
							</text>
						</svg>
					</div>
					
					<table id="thumbDetailTable" border="1" cellspacing="1" style="align:center">
						<thead>
							<tr>
								<th class="titleCell" onclick="sortTable('thumbDetailTable', 0, 'String')" ><div style="cursor:n-resize">Date</div></th>
								<th class="titleCell" onclick="sortTable('thumbDetailTable', 1, 'String')" ><div style="cursor:n-resize">Mode</div></th>
								<th class="titleCell" onclick="sortTable('thumbDetailTable', 2, 'int')" ><div style="cursor:n-resize">Count</div></th>
								<th class="titleCell" onclick="sortTable('thumbDetailTable', 3, 'float')" ><div style="cursor:n-resize">Size(KB)</div></th>
								<th class="titleCell" onclick="sortTable('thumbDetailTable', 4, 'float')" ><div style="cursor:n-resize">Time(S)</div></th>
								<th class="titleCell" onclick="sortTable('thumbDetailTable', 5, 'float')" ><div style="cursor:n-resize">Speed(KB/S)</div></th>
							</tr>
						</thead>
						<tbody>
							<xsl:for-each select="/statistic/thumb">
							<tr>
								<xsl:if test="@mode = 'sql'">
									<td class="sqlCell"><xsl:value-of select="@date-string" /></td>
									<td class="sqlCell"><xsl:value-of select="translate(@mode, $lowercase, $uppercase)" /></td>
									<td class="sqlCell"><xsl:value-of select="@count" /></td>
									<td class="sqlCell"><xsl:value-of select="format-number((@size div 1000),'0.0')" /></td>
									<td class="sqlCell"><xsl:value-of select="format-number((@time div 1000),'0.000')" /></td>
									<td class="sqlCell"><xsl:value-of select="format-number((@size div @time),'0.00')" /></td>
								</xsl:if>
								<xsl:if test="@mode = 'db4o'">
									<td class="db4oCell"><xsl:value-of select="@date-string" /></td>
									<td class="db4oCell"><xsl:value-of select="translate(@mode, $lowercase, $uppercase)" /></td>
									<td class="db4oCell"><xsl:value-of select="@count" /></td>
									<td class="db4oCell"><xsl:value-of select="format-number((@size div 1000),'0.0')" /></td>
									<td class="db4oCell"><xsl:value-of select="format-number((@time div 1000),'0.000')" /></td>
									<td class="db4oCell"><xsl:value-of select="format-number((@size div @time),'0.00')" /></td>
								</xsl:if>
							</tr>
							</xsl:for-each>
						</tbody>
					</table>
				</div>
				<div id="upload" style="vertical-align:middle;float:left">
					<xsl:variable name="sqlSize"
						select="sum(/statistic/upload[@mode='sql']/@size)" />
					<xsl:variable name="db4oSize"
						select="sum(/statistic/upload[@mode='db4o']/@size)" />
					<xsl:variable name="sqlCount"
						select="sum(/statistic/upload[@mode='sql']/@count)" />
					<xsl:variable name="db4oCount"
						select="sum(/statistic/upload[@mode='db4o']/@count)" />
					<xsl:variable name="sqlTime"
						select="sum(/statistic/upload[@mode='sql']/@time)" />
					<xsl:variable name="db4oTime"
						select="sum(/statistic/upload[@mode='db4o']/@time)" />
					<xsl:variable name="sqlSpeed"
						select="format-number(($sqlSize div $sqlTime),'0.00')" />
					<xsl:variable name="db4oSpeed"
						select="format-number(($db4oSize div $db4oTime),'0.00')" />
					<xsl:variable name="maxSpeed">
						<xsl:choose>
							<xsl:when test="$sqlSpeed &gt; $db4oSpeed">
								<xsl:value-of select="$sqlSpeed" />
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$db4oSpeed" />
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<xsl:variable name="sqlHeight" select="$sqlSpeed div $maxSpeed * 200" />
					<xsl:variable name="db4oHeight" select="$db4oSpeed div $maxSpeed * 200" />
					<div>2.Uploading</div>

					<div>
						<table border="1">
							<th></th>
							<th>SQL(Oracle)</th>
							<th>Object(DB4O)</th>
							<tr>
								<td>Load Size(Byte)</td>
								<td>
									<xsl:value-of select="$sqlSize" />
								</td>
								<td>
									<xsl:value-of select="$db4oSize" />
								</td>
							</tr>
							<tr>
								<td>Count(full)</td>
								<td>
									<xsl:value-of select="$sqlCount" />
								</td>
								<td>
									<xsl:value-of select="$db4oCount" />
								</td>
							</tr>
							<tr>
								<td>Load Time(ms)</td>
								<td>
									<xsl:value-of select="$sqlTime" />
								</td>
								<td>
									<xsl:value-of select="$db4oTime" />
								</td>
							</tr>
							<tr>
								<td>Speed(KB/s)</td>
								<td>
									<xsl:value-of select="$sqlSpeed" />
								</td>
								<td>
									<xsl:value-of select="$db4oSpeed" />
								</td>
							</tr>
						</table>
					</div>
					<div class="svg" style="width:450px;height:300px;">
						<svg width="100%" height="100%" version="1.1"
							xmlns="http://www.w3.org/2000/svg">
							<line x1="1" y1="30" x2="1" y2="250"
								style="stroke:rgb(5,15,192);stroke-width:1" />
							<line x1="1" y1="250" x2="350" y2="250"
								style="stroke:rgb(5,15,192);stroke-width:1" />
							<text x="0" y="20">
								Speed(KB/s)
							</text>
							<rect x="90" y="{50+200-$sqlHeight}" width="50" height="{$sqlHeight}"
								style="fill:blue;stroke:black;stroke-width:0;fill-opacity:1;stroke-opacity:0.9" />
							<text x="100" y="270">
								SQL
							</text>
							<text x="95" y="{45+200-$sqlHeight}">
								<xsl:value-of select="$sqlSpeed" />
							</text>
							<rect x="230" y="{50+200-$db4oHeight}" width="50" height="{$db4oHeight}"
								style="fill:orange;stroke:black;stroke-width:0;fill-opacity:1;stroke-opacity:0.9" />
							<text x="240" y="270">
								DB4O
							</text>
							<text x="235" y="{45+200-$db4oHeight}">
								<xsl:value-of select="$db4oSpeed" />
							</text>
						</svg>
					</div>
					<table id="uploadDetailTable" border="1" cellspacing="1" style="align:center">
						<thead>
							<tr>
								<th class="titleCell" onclick="sortTable('uploadDetailTable', 0, 'String')" ><div style="cursor:n-resize">Date</div></th>
								<th class="titleCell" onclick="sortTable('uploadDetailTable', 1, 'String')"><div style="cursor:n-resize">Mode</div></th>
								<th class="titleCell" onclick="sortTable('uploadDetailTable', 2, 'int')"><div style="cursor:n-resize">Count</div></th>
								<th class="titleCell" onclick="sortTable('uploadDetailTable', 3, 'float')"><div style="cursor:n-resize">Size(KB)</div></th>
								<th class="titleCell" onclick="sortTable('uploadDetailTable', 4, 'float')"><div style="cursor:n-resize">Time(S)</div></th>
								<th class="titleCell" onclick="sortTable('uploadDetailTable', 5, 'float')"><div style="cursor:n-resize">Speed(KB/S)</div></th>
							</tr>
						</thead>
						<tbody>
						<xsl:for-each select="/statistic/upload">
							<tr>
								<xsl:if test="@mode = 'sql'">
									<td class="sqlCell"><xsl:value-of select="@date-string" /></td>
									<td class="sqlCell"><xsl:value-of select="translate(@mode, $lowercase, $uppercase)" /></td>
									<td class="sqlCell"><xsl:value-of select="@count" /></td>
									<td class="sqlCell"><xsl:value-of select="format-number((@size div 1000),'0.0')" /></td>
									<td class="sqlCell"><xsl:value-of select="format-number((@time div 1000),'0.000')" /></td>
									<td class="sqlCell"><xsl:value-of select="format-number((@size div @time),'0.00')" /></td>
								</xsl:if>
								<xsl:if test="@mode = 'db4o'">
									<td class="db4oCell"><xsl:value-of select="@date-string" /></td>
									<td class="db4oCell"><xsl:value-of select="translate(@mode, $lowercase, $uppercase)" /></td>
									<td class="db4oCell"><xsl:value-of select="@count" /></td>
									<td class="db4oCell"><xsl:value-of select="format-number((@size div 1000),'0.0')" /></td>
									<td class="db4oCell"><xsl:value-of select="format-number((@time div 1000),'0.000')" /></td>
									<td class="db4oCell"><xsl:value-of select="format-number((@size div @time),'0.00')" /></td>
								</xsl:if>
							</tr>
						</xsl:for-each>
						</tbody>
					</table>
				</div>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>